﻿using System;
using System.Net;
using System.Collections.Generic;
using System.IO;

namespace WaterRegimenMonitor.Common.Pinyin
{
    /// <summary>
    /// 汉字字符字典
    /// </summary>
    /// <remarks>用于保存汉字字符信息。</remarks>
    /// <history>
    /// [zanezeng]               2010/05/27 14:57    创建
    /// </history>
    public class CharDictionary
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="CharDictionary"/>的新实例
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:05    创建
        /// </history>
        private CharDictionary()
        {
        }

        #endregion

        #region ---Public Method

        /// <summary>
        /// 从给定的二进制读取器重加载字符字典
        /// </summary>
        /// <param name="binaryReader">包含字符字典信息的二进制读取器。</param>
        /// <returns>从给定的二进制读取器重加载的字符字典</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:06    创建
        /// </history>
        public static CharDictionary Load(BinaryReader binaryReader)
        {
            //创建字符字典s
            CharDictionary dictionary = new CharDictionary();

            //读取整数
            binaryReader.ReadInt32();

            //读取内容长度
            dictionary.m_Length = binaryReader.ReadInt32();
            //读取字符单元总数
            dictionary.m_Count = binaryReader.ReadInt32();
            //读取偏移值
            dictionary.m_Offset = binaryReader.ReadInt16();

            //读取保留值
            binaryReader.ReadBytes(dictionary.RESERVED.Length);

            //创建字符单元表
            dictionary.m_CharUnitTable = new List<CharUnit>();

            //循环读取字符单元
            for (int i = 0; i < dictionary.m_Count; i++)
            {
                //读取字符单元
                dictionary.m_CharUnitTable.Add(CharUnit.Load(binaryReader));
            }

            //读取结束掩码
            binaryReader.ReadInt16();

            //返回字符字典
            return dictionary;
        }

        /// <summary>
        /// 获得给定字符对应的字符单元
        /// </summary>
        /// <param name="item">要获得字符单元的字符。</param>
        /// <returns>给定字符对应的字符单元。如果不存在，则返回<c>null</c>引用。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:26    创建
        /// </history>
        public CharUnit GetCharUnit(char item)
        {
            //循环检索
            foreach (CharUnit unit in this.m_CharUnitTable)
            {
                //比较
                if (unit.Char == item)
                {
                    //返回字符单元
                    return unit;
                }
            }

            //默认返回空引用
            return null;
        }

        /// <summary>
        /// 获得给定序号的字符单元
        /// </summary>
        /// <param name="index">要获得的字符单元的序号。</param>
        /// <returns>对应序号的字符单元。</returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/>小于0或者大于字符单元字典的有效范围。</exception>
        /// <history>
        /// [zanezeng]               2010/05/27 15:27    创建
        /// </history>
        public CharUnit GetCharUnit(int index)
        {
            //返回
            return this.m_CharUnitTable[index];
        }

        /// <summary>
        /// 将字符单元字典保存到给定的二进制写入流中。
        /// </summary>
        /// <param name="binaryWriter">用于保存字符单元字典的二进制写入流。</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:29    创建
        /// </history>
        public void Save(BinaryWriter binaryWriter)
        {
            //写入长度
            binaryWriter.Write(this.m_Length);

            //写入总数
            binaryWriter.Write(this.m_Count);

            //写入偏移值
            binaryWriter.Write(this.m_Offset);

            //写入保留值
            binaryWriter.Write(this.RESERVED);

            //循环写入字符单元
            for (int i = 0; i < this.m_Count; i++)
            {
                //写入字符单元
                this.m_CharUnitTable[i].Save(binaryWriter);
            }

            //写入结束掩码
            binaryWriter.Write(this.EndMark);
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得当前字符字典中字符的总数。
        /// </summary>
        /// <value>当前字符字典中字符的总数。</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:33    创建
        /// </history>
        public int Count
        {
            get
            {
                return this.m_Count;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 汉字字符单元表
        /// </summary>
        private List<CharUnit> m_CharUnitTable;

        /// <summary>
        /// 字符单元的总数
        /// </summary>
        private int m_Count;

        /// <summary>
        /// 结束掩码
        /// </summary>
        private readonly short EndMark = 32767;

        /// <summary>
        /// 总长度
        /// </summary>
        private int m_Length;

        /// <summary>
        /// 偏移值
        /// </summary>
        private short m_Offset;

        /// <summary>
        /// 保留值
        /// </summary>
        private readonly byte[] RESERVED = new byte[24];

        #endregion
    }
}
